/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.module.background.controller;

import com.github.lvyanyang.annotation.Authorize;
import com.github.lvyanyang.core.BaseWebController;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.XCI;
import com.github.lvyanyang.sys.service.SysParamService;
import com.xci.edu.core.entity.EduStudent;
import com.xci.edu.core.entity.ExamPre;
import com.xci.edu.core.filter.EduStudentFilter;
import com.xci.edu.core.service.EduStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 学员信息页面控制器
 * @author 张谦 zhangqian@xci96716.com
 * @since 2020-11-24 15:29:29
 */
@Authorize
@Controller
@RequestMapping(value = "/background/eduStudent")
public class EduStudentController extends BaseWebController {
    /** 学员信息服务 */
    @Resource private EduStudentService eduStudentService;
    @Resource private SysParamService paramService;

    //region 页面

    /** 首页 */
    @GetMapping
    @Authorize(code = "background.eduStudent")
    public String index() {
        return "background/eduStudent/index";
    }

    /** 新建页 */
    @GetMapping("/create")
    @Authorize(code = "background.eduStudent.insert")
    public String create(ModelMap map) {
        createMark(map);
        var entity = new EduStudent();
        entity.setId(XCI.nextId());
        map.put("entity", entity);
        return "background/eduStudent/edit";
    }

    /** 编辑页 */
    @GetMapping("/edit")
    @Authorize(code = "background.eduStudent.update")
    public String edit(String id, ModelMap map) {
        var entity = eduStudentService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/eduStudent/edit";
    }

    /** 详情页 */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        var entity = eduStudentService.selectById(Long.valueOf(id));
        XCI.checkNotFoundData(entity, id);
        map.put("entity", entity);
        return "background/eduStudent/details";
    }

    /** 预约考试页 */
    @GetMapping("/setExamPro")
    @Authorize(code = "background.eduStudent.setExamPro")
    public String setExamPro(String ids,String categoryId,ModelMap map) {
        map.put("ids", ids);
        map.put("categoryId", categoryId);
        //获取系统参数---业务-预约考试可考次数
        String usableCount = paramService.getByCodeFromCache("exam.pre.usableCount").getString("2");
        map.put("usableCount", usableCount);
        return "background/eduStudent/setExamPro";
    }

    //endregion

    //region 处理

    /** 表格查询 */
    @ResponseBody
    @PostMapping("/grid")
    @Authorize(code = "background.eduStudent.select")
    public BoolResult grid(EduStudentFilter filter) {
        return BoolResult.success(eduStudentService.selectPageList(filter));
    }

    /** 预报名 */
    @ResponseBody
    @PostMapping("/beforehandCreateSave")
    @Authorize(code = "background.eduStudent.beforehandInsert")
    public BoolResult beforehandCreateSave(@ModelAttribute EduStudent entity) {
        entity.setStatus(0);
        return eduStudentService.insert(entity);
    }

    /** 报名 */
    @ResponseBody
    @PostMapping("/createSave")
    @Authorize(code = "background.eduStudent.insert")
    public BoolResult createSave(@ModelAttribute EduStudent entity) {
        entity.setStatus(1);
        return eduStudentService.insert(entity);
    }

    /** 修改保存 */
    @ResponseBody
    @PostMapping("/updateSave")
    @Authorize(code = "background.eduStudent.update")
    public BoolResult updateSave(@ModelAttribute EduStudent entity) {
        return eduStudentService.update(entity);
    }

    /** 删除 */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "background.eduStudent.delete")
    public BoolResult delete(String ids) {
        return eduStudentService.delete(ids);
    }

    /** 导出 */
    @GetMapping("/export")
    @Authorize(code = "background.eduStudent.export")
    public void export(EduStudentFilter filter) {
        XCI.exportExcel(eduStudentService.selectList(filter), EduStudent.class, "学员信息列表");
    }

    /** 学生信息审核 */
    @ResponseBody
    @PostMapping("/auditStudent")
    @Authorize(code = "background.eduStudent.auditStudent")
    public BoolResult auditStudent(String ids) {
        return eduStudentService.auditStudent(ids);
    }

    /** 退档 */
    @ResponseBody
    @PostMapping("/setStatusOne")
    @Authorize(code = "background.eduStudent.setStatusOne")
    public BoolResult setStatusOne(String ids) {
        return eduStudentService.setStatusOne(ids);
    }

    /** 预约考试 */
    @ResponseBody
    @PostMapping("/setExamProUpdate")
    @Authorize(code = "background.eduStudent.setExamPro")
    public BoolResult setExamProUpdate(String ids, @ModelAttribute ExamPre entity) {
        return eduStudentService.setExamProUpdate(ids,entity);
    }
    //endregion
}